﻿using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Schema.Extensibility;
using Microsoft.Data.Schema.SchemaModel;
using Microsoft.Data.Schema.ScriptDom.Sql;
using Microsoft.Data.Schema.Sql.SchemaModel;
using Microsoft.Data.Schema.Sql;
using Microsoft.Data.Schema.StaticCodeAnalysis;


namespace Neznayka
{
    public static class DMVRuleSetup
    {
        public static void RuleSetup(DataRuleExecutionContext context, out List<DataRuleProblem> problems, out SqlSchemaModel sqlSchemaModel, out ISqlModelElement sqlElement, out TSqlFragment sqlFragment)
        {
            DMVSettings.LoadSettings();
            // Get Model collation 
            SqlComparer.Comparer = context.SchemaModel.Comparer;

            problems = new List<DataRuleProblem>();

            IModelElement modelElement = context.ModelElement;

            // casting to SQL specific 
            sqlSchemaModel = modelElement.Model as SqlSchemaModel;
            Debug.Assert(sqlSchemaModel != null, "SqlSchemaModel is expected");

            sqlElement = modelElement as ISqlModelElement;
            Debug.Assert(sqlElement != null, "ISqlModelElement is expected");

            // Get ScriptDom for this model element
            sqlFragment = context.ScriptFragment as TSqlFragment;
            Debug.Assert(sqlFragment != null, "TSqlFragment is expected");
        }

        public static void RefreshDDLCache(ModelStore sqlSchemaModel)
        {
            DMVSettings.RefreshColumnCache(sqlSchemaModel);
            DMVSettings.RefreshConstraintsAndIndexesCache(sqlSchemaModel);
        }

        public static void getOwningObject(IModelElement sqlElement, out string owningObjectSchema, out string owningObjectTable)
        {
            owningObjectSchema = sqlElement.Name.Parts[0];
            owningObjectTable = sqlElement.Name.Parts[1];
        }


    }
}
